import { getStore } from './storage';

/*const hasRole = {
    install (Vue, options) {
        Vue.directive('hasRole', {
            inserted (el, binding) {
                let roles = getStore("roles");
                if (!roles.includes(binding.value)) {
                    el.parentNode.removeChild(el);
                }
            }
        });
    }
};*/
// 必须包含列出的所有角色，元素才显示
export const hasRole = {
    install (Vue) {
        Vue.directive('hasRole', {
            inserted (el, binding, vnode) {
                let roles = getStore("roles");
                let value = binding.value.split(',');
                let flag = true;
                for (let v of value) {
                    if (!roles.includes(v)) {
                        flag = false
                    }
                }
                if (!flag) {
                    el.parentNode.removeChild(el)
                }
            }
        })
    }
}

// 只要包含列出的任意一个角色，元素就会显示
export const hasAnyRole = {
    install (Vue) {
        Vue.directive('hasAnyRole', {
            inserted (el, binding, vnode) {
                let roles = getStore("roles");
                let value = binding.value.split(',')
                let flag = false
                for (let v of value) {
                    if (roles.includes(v)) {
                        flag = true
                    }
                }
                if (!flag) {
                    el.parentNode.removeChild(el)
                }
            }
        })
    }
}

//export default hasRole;
